<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coverImg">
              <j-image-upload isMultiple v-model="model.coverImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeImg">
              <j-image-upload isMultiple v-model="model.storeImg" :number="9"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">一次上传可选择多张图片！请一次性上传完所需图片！</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeName">
              <a-input v-model="model.storeName" placeholder="请输入门店名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维修范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeRange">
              <a-textarea v-model="model.storeRange" rows="4" placeholder="请输入维修范围" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradeTime">
              <a-input v-model="model.tradeTime" placeholder="请输入营业时间"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contacts">
              <a-input v-model="model.contacts" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsPhone">
              <a-input v-model="model.contactsPhone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeAddress">
              <a-input v-model="model.storeAddress" placeholder="请输入门店地址" @blur="addrHand"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店经纬度(经度)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input v-model="model.longitude" placeholder="请输入门店地址，获取经纬度(经度)" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门店经纬度(纬度)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input v-model="model.latitude" placeholder="请输入门店地址，获取经纬度(纬度)" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="门店地址(定位)" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="storeAddressPositioning">
              <a-input v-model="model.storeAddressPositioning" placeholder="请输入门店地址(定位)"></a-input>
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="门店介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeIntroduce">
              <a-textarea v-model="model.storeIntroduce" rows="4" placeholder="请输入门店介绍" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="详情介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describes">
              <j-editor v-model="model.describes"></j-editor>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <!-- <a-input v-model="model.type" placeholder="请输入状态"></a-input> -->
              <a-radio-group v-model="model.type" placeholder="请选择状态">
                <a-radio value="1">审核中</a-radio>
                <a-radio value="2">未通过</a-radio>
                <a-radio value="3">通过</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="失败原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reasons">
              <a-input v-model="model.reasons" placeholder="请输入失败原因"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { axios } from '@/utils/request'
import { jsonp } from 'vue-jsonp'
export default {
  name: 'MachineMaintainStoreForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        coverImg: [{ required: true, message: '请上传封面图' }],
        storeImg: [{ required: true, message: '请上传门店图片' }],
        storeName: [{ required: true, message: '请输入门店名称' }],
        storeRange: [{ required: true, message: '请输入维修范围' }],
        tradeTime: [{ required: true, message: '请输入营业时间' }],
        contacts: [{ required: true, message: '请输入联系人' }],
        contactsPhone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        storeAddress: [{ required: true, message: '请输入门店地址' }],
        longitude: [{ required: true, message: '请输入门店经纬度(经度)' }],
        latitude: [{ required: true, message: '请输入门店经纬度(纬度)' }],
        // storeIntroduce: [{ required: true, message: '请输入门店介绍' }],
        describes: [{ required: true, message: '请输入详情介绍' }]
      },
      url: {
        add: "/maintain/machineMaintainStore/add",
        edit: "/maintain/machineMaintainStore/edit",
        queryById: "/maintain/machineMaintainStore/queryById"
      },
      // 替换为你的腾讯地图API密钥  
      tencentMapKey: 'BANBZ-VEQC2-24OUC-CHMOJ-YRKSH-KGFXO'
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    //input失焦获取填写地址
    async addrHand() {
      if (!this.model.storeAddress) return;
      console.log('进来了', this.model.storeAddress, this.tencentMapKey);
      // const url = `https://apis.map.qq.com/ws/geocoder/v1/?address=${this.model.storeAddress}&key=${this.tencentMapKey}&output='jsonp'`;
      // try {
      //   const response = await axios.get(url);
      //   console.log('地图信息', response);
      //   const { result } = response.data;
      //   if (result && result.location) {
      //     this.latitude = result.location.lat;
      //     this.longitude = result.location.lng;
      //   } else {
      //     console.error('无法获取经纬度信息');
      //   }
      // } catch (error) {
      //   console.error('请求失败:', error);
      // }
      // let that = this
      // axios({
      //   type: "get",
      //   dataType: 'jsonp',
      //   data: {
      //     key: "BANBZ-VEQC2-24OUC-CHMOJ-YRKSH-KGFXO", // 填申请到的腾讯key
      //     address: that.model.storeAddress, //具体的地址
      //     output: 'jsonp' //返回格式：支持JSON/JSONP，默认JSON
      //   },
      //   jsonp: "callback",
      //   jsonpCallback: "QQmap",
      //   url: "https://apis.map.qq.com/ws/geocoder/v1/?",
      //   success: function (json) {
      //     console.log('地图信息', json)
      //     if (json.status == 0) {
      //       that.form.latitude = json.result.location.lat
      //       that.form.longitude = json.result.location.lng
      //       that.$message({
      //         message: '成功获取位置的经纬度',
      //         type: 'success'
      //       })
      //     } else {
      //       that.$message.error('获取该位置经纬度失败')
      //     }
      //   },
      //   error: function (err) {
      //     that.$message.error('异常错误，请刷新浏览器后重试')
      //   }
      // })
      jsonp(`https://apis.map.qq.com/ws/geocoder/v1/`, {
        output: 'jsonp',
        key: this.tencentMapKey,
        address: this.model.storeAddress
      }).then((res) => {
        // console.log('地图信息', res);
        if (res.status == 0) {
          const location = res.result.location;
          if (location) {
            this.model.longitude = location.lng
            this.model.latitude = location.lat
          }
        } else if (res.status == 348) {
          this.$message.error('请输入正确的地址信息');
          this.model.longitude = ''
          this.model.latitude = ''
        } else {
          this.$message.error(res.message);
        }
      })
    },
  }
}
</script>